package jcommon.chap9_templating.velocity.demoframework;

import java.io.Reader;
import java.io.StringWriter;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

public abstract class VelocityDemo {

	public final void execute() {
		StringWriter writer = new StringWriter();
		try {
			processWriterByVolicity(writer);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		System.out.println(writer.toString());
	}

	/**
	 * 功能: 子类可以修改这个方法决定如何通过模板生成数据
	 * @author 林可  2010-5-5 下午03:52:56
	 * @param writer
	 * @throws Exception
	 */
	protected void processWriterByVolicity(StringWriter writer)
			throws Exception {
		getVelocityEngine().evaluate(getVelocityContext(), writer, "test",
				getTemplateReader());
	}

	/**
	 * 功能: 返回VelocityEngine
	 * 
	 * @author 林可 2010-5-5 下午02:48:35
	 * @return
	 */
	protected abstract VelocityEngine getVelocityEngine();

	/**
	 * 功能: Create a context and put object carrying print information into the
	 * context
	 * 
	 * @author 林可 2010-5-5 上午10:07:13
	 * @return VelocityContext
	 */
	protected abstract VelocityContext getVelocityContext();

	/**
	 * 功能: Create a Reader to read velocity template
	 * 
	 * @author 林可 2010-5-5 上午10:09:53
	 * @return Reader
	 */
	protected abstract Reader getTemplateReader();
}
